package com.senior.atomic;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

public class AtomicIntegerFieldUpdateDemo {
    public static void main(String[] args) throws InterruptedException {
        BankAccount bankAccount = new BankAccount();
        CountDownLatch countDownLatch = new CountDownLatch(1000);
        for (int i = 1; i <=1000 ; i++) {
            new Thread(()->{
                bankAccount.transfer(bankAccount);
                countDownLatch.countDown();
            },i+"").start();
        }
        countDownLatch.await();
        System.out.println(Thread.currentThread().getName() + "---:" + bankAccount.money);
    }

}

class BankAccount{

    String bankName = "abc";

    public volatile int money = 0;

    AtomicIntegerFieldUpdater atomicIntegerFieldUpdater =
            AtomicIntegerFieldUpdater.newUpdater(BankAccount.class,"money");

    public void transfer(BankAccount bankAccount){
        atomicIntegerFieldUpdater.incrementAndGet(bankAccount);
    }


}